package com.example.adtest

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Adapter
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder


class LogAdapter(logList:ArrayList<LogBean>,context: Context): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var logList = logList
    private var  context = context


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val inflate = LayoutInflater.from(context).inflate(R.layout.item_list, parent, false)
        return MyViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return logList.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        // 将holder转换为MyViewHolder类型
        val myHolder = holder as MyViewHolder
        // 获取当前位置的LogBean对象
        val logBean = logList[position]
        
        // 设置标题，包含ecpm信息
        myHolder.newsTitle.text = "${logBean.title}   ${logBean.ecpm}"
        // 设置时间
        myHolder.time.text = logBean.time
    }

    class MyViewHolder (itemView:View)
        : RecyclerView.ViewHolder(itemView) {
        //绑定适配器ID获取适配器视图
        val  newsTitle: TextView = itemView.findViewById<TextView>(R.id.title)
        val  time: TextView = itemView.findViewById(R.id.time)
    }
    
    // 添加更新数据的方法
    fun updateData(newLogList: ArrayList<LogBean>) {
        this.logList = newLogList
        notifyDataSetChanged()
    }
    
    // 添加单条数据的方法
    fun addLog(logBean: LogBean) {
        logList.add(logBean)
        notifyItemInserted(logList.size - 1)
    }
}